const Base = require('./base.js');
const fs = require('fs');
const moment = require('moment');
const axios = require('axios');

module.exports = class extends Base {
    /**
     *
     * @returns {Promise<void>}
     */
    async cancelAction() {
        const result = await this.model('appointment').where({
            user_id: think.user.user_id,
            id: this.post('appointment_id')
        }).update({status: -1});
        return this.success('取消成功');
    }

    /**
     * 客户预约上面量衣
     * @returns {Promise<void>}
     */
    async addAction() {
        const d = this.post();
        const result = await this.model('appointment').add({
            user_id: think.user.user_id,
            appoint_time: d.appoint_time || '',
            username: d.username,
            pro_id: d.pro_id || 0,
            appoint_no: moment().format('YYYYMMDDHHmmss') + Math.random().toString().substr(2, 6),
            phone: d.phone,
            area: d.area,
            address: d.address,
            province: d.province,
            city: d.city,
            parter_id: 0,
            update_time: moment().format('YYYY-MM-DD HH:mm:ss')
        });
        return this.success('预约成功');
    }

    /**
     * 客户预约上面量衣
     * @returns {Promise<void>}
     */
    async updateAction() {
        const d = this.post();
        const result = await this.model('appointment').where({
            user_id: think.user.user_id,
            id: d.appointment_id
        }).update({
            appoint_time: d.appoint_time || '',
            username: d.username,
            phone: d.phone,
            area: d.area,
            address: d.address,
            update_time: moment().format('YYYY-MM-DD HH:mm:ss')
        });
        return this.success(result);
    }

    /**
     * 预约记录
     * @returns {Promise<void>}
     */
    async recordAction() {
        const d = this.post();
        const result = await this.model('appointment').where({
            user_id: think.user.user_id, status: ['>', -1]
        }).page(d.page || 1, d.limit || 30).order('add_time desc').countSelect();
        return this.success(result);
    }

    /**
     *
     * @returns {Promise<void>}
     */
    async infoAction() {
        const d = this.post();
        const result = await this.model('appointment').where({
            id: d.appointment_id
        }).find();
        return this.success(result);
    }

    /**
     * 客户预约数
     * @returns {Promise<void>}
     */
    async countAction() {
        const result = await this.model('appointment').count();
        return this.success(result);
    }
};
